local zishucup__shanglve = fk.CreateSkill{
  name = "zishucup__shanglve",
}

Fk:loadTranslationTable{
  ["zishucup__shanglve"] = "上略",
  [":zishucup__shanglve"] = "你对你上次使用牌的目标角色的上下家使用牌无距离次数限制，且若你本阶段自使用第一张牌后皆如此做，你摸一张牌。"..
  "<font color=\"grey\">皆如此做：你使用牌指定了你上次使用牌的目标角色的上家或下家</font>",

  ["@[player]zishucup__shanglve"] = "上略",

  ["$zishucup__shanglve1"] = "兼文武之上略，怀济世之弘规。",
  ["$zishucup__shanglve2"] = "深算利害，独克制敌，终至禽贼。",
}

zishucup__shanglve:addEffect(fk.EventPhaseStart, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(zishucup__shanglve.name) and player.phase == Player.Finish
  end,
  on_use = function(self, event, target, player, data)
    while not player.dead do
      player:drawCards(1, zishucup__shanglve.name)
      if not table.find(player.room:getOtherPlayers(player), function(p) return p:getHandcardNum() == player:getHandcardNum() end) then
        break
      end
    end
  end,
})

zishucup__shanglve:addEffect(fk.CardUsing, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(zishucup__shanglve.name) and table.find(player.room:getAllPlayers(),
      function(p) return p:getHandcardNum() == player:getHandcardNum() and not p:isNude() end)
  end,
  on_cost = function(self, event, target, player, data)
    local list = table.filter(player.room:getAllPlayers(), function(p) return p:getHandcardNum() == player:getHandcardNum() and not p:isNude() end)
    if #list == 1 then
      event:setCostData(self, {tos = list[1]})
      return true
    else
      local to = player.room:askToChoosePlayers(player, {
        min_num = 1,
        max_num = 1,
        targets = list,
        skill_name = zishucup__shanglve.name,
        cancelable = false,
        prompt = "#zishucup__buqun-choose",
      })
      event:setCostData(self, {tos = to[1]})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local to = event:getCostData(self).tos
    local chosen = player.room:askToChooseCard(player, { target = to, flag = "he", skill_name = zishucup__shanglve.name })
    to:addToPile("$zishucup__buqun", {chosen}, nil, zishucup__shanglve.name)
  end,
})

zishucup__shanglve:addEffect("targetmod", {
  is_delay_effect = true,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return not player.dead and #player:getPile("$zishucup__buqun") > 0
  end,
  on_use = function(self, event, target, player, data)
    player.room:moveCardTo(player:getPile("$zishucup__buqun"), Player.Hand, player, fk.ReasonPrey, zishucup__shanglve.name)
  end,
})

return zishucup__shanglve
